<template>
  <div class="login-container">
    <el-row class="title">福袋星球渠道后台</el-row>
    <el-row class="login-wrapper" justify="center">
      <el-row class="login-form-wrapper">
        <el-row justify="center" class="login-img-wrapper">
          <div class="login-img"></div>
        </el-row>
        <div class="form">
          <div class="login-title">Hi~欢迎回来，请登录</div>
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
          >
            <el-form-item prop="account">
              <el-input
                clearable
                v-model="loginForm.account"
                placeholder="请输入账号"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                clearable
                v-model="loginForm.password"
                placeholder="请输入密码"
                show-password
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-checkbox
                label="记住登录账号"
                v-model="accountLock"
                @change="accountLockChange"
              ></el-checkbox>
            </el-form-item>
            <el-form-item>
              <el-button
                style="width: 100%"
                :disabled="!loginEnable"
                :type="loginEnable ? 'primary' : ''"
                :class="loginEnable ? '' : 'login-btn'"
                @click="submitForm"
                >登录</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </el-row>
    </el-row>
  </div>
</template>

<script>
import { computed, reactive, ref, toRaw } from "vue";
import { useRouter, useRoute } from "vue-router";
import { login } from "@/api/api.js";
import _ from "lodash";
import { useStore } from "vuex";
const loginRules = {
  account: {
    required: true,
    message: "请输入账号",
  },
  password: [
    {
      required: true,
      message: "请输入密码",
    },
    { min: 8, max: 32, message: "密码长度为8-32位" },
  ],
};
export default {
  setup() {
    const store = useStore();
    const router = useRouter();
    const route = useRoute();
    //登录表单
    const _loginForm = store.state.accountInfo
      ? _.cloneDeep(toRaw(store.state.accountInfo))
      : {
          account: "",
          password: "",
        };
    const loginForm = reactive(_loginForm);
    const loginFormRef = ref(null);
    const loginEnable = computed(() => {
      return loginForm.account && loginForm.password ? true : false;
    });
    //记住登录账号
    const accountLock = ref(store.state.accountInfo ? true : false);
    const accountLockChange = (value) => {
      console.log("accountLockChange", value);
      accountLock.value = value;
    };
    //提交登录
    const submitForm = () => {
      loginFormRef.value.validate(async (valid) => {
        if (valid) {
          console.log("submit success!");
          try {
            const {
              data: { data },
            } = await login({
              username: loginForm.account,
              password: loginForm.password,
            });
            //登录成功
            //记住登录账号
            if (accountLock.value) {
              store.dispatch(
                "changeAccountInfo",
                JSON.stringify(toRaw(loginForm))
              );
            } else {
              store.dispatch("changeAccountInfo", "");
            }
            //登录
            store.dispatch("login", data.token);
            //路由跳转
            if (route.query.back) {
              router.replace({ path: route.query.back });
            } else {
              router.replace({ path: "/" });
            }
          } catch (error) {
            console.log("error is", error);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    };

    return {
      loginForm,
      loginEnable,
      loginFormRef,
      accountLock,
      accountLockChange,
      loginRules,
      submitForm,
    };
  },
};
</script>

<style lang="less" scoped>
.login-container {
  padding-top: 100px;
  height: 100%;
  box-sizing: border-box;
  background: url("../assets/img/login_bg.jpg") center center;
  .title {
    padding-left: 200px;
    font-size: 40px;
    font-weight: bold;
    color: @fontColor;
  }
  .login-wrapper {
    margin-top: 100px;
    .login-form-wrapper {
      background: #fff;
      box-shadow: 2px 2px 2px 2px #fff;
      border-radius: 21px;
      padding: 39px 40px 0 20px;
      .login-img-wrapper {
        width: 352px;
        height: 352px;
        text-align: center;
        margin-top: 24px;
        margin-right: 50px;
        .login-img {
          width: 282px;
          height: 334px;
          background: url("../assets/img/login_box.png");
          background-size: 100% 100%;
        }
      }
      .login-title {
        font-size: 24px;
        font-weight: bold;
        color: @fontColor;
      }
      .login-form {
        margin-top: 15px;
        .login-btn {
          width: 100%;
          color: #ffffff;
          background-color: #c8c9cc;
          border-color: #c8c9cc;
        }
      }
    }
  }
}
</style>
